package org.kjk.mail;

import java.io.IOException;
import java.util.Date;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.MimeMultipart;

public class GMailDTO {
	public String sender;
	public String subject;
	public Object content;
	public Date receivedDate;
	public int pageIndex;

	public GMailDTO(String sender, String subject, Object content,
			Date receivedDate, int index) throws IOException,
			MessagingException {
		this.sender = sender;
		this.subject = subject;
		if (content instanceof javax.mail.internet.MimeMultipart) {
			this.content = ((MimeMultipart) content).getPreamble();
		} else {
			this.content = content;
		}
		this.pageIndex = index;
		this.receivedDate = receivedDate;
	}

	public GMailDTO(Message message, int index) throws Exception {
		StringBuilder str = new StringBuilder();
		this.pageIndex = index;
		for (Address addr : message.getAllRecipients()) {
			str.append(addr.toString());
		}
		this.sender = str.toString();
		this.subject = message.getSubject();
		if (message.getContent() instanceof javax.mail.internet.MimeMultipart) {
//			this.content = ((MimeMultipart) message.getContent())
//					.getBodyPart(0);
			this.content = dumpPart(message);
		} else {
			this.content = message.getContent();
		}
		this.receivedDate = message.getReceivedDate();
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public Object getContent() {
		return content;
	}

	public void setContent(Object content) {
		this.content = content;
	}

	public Date getReceivedDate() {
		return receivedDate;
	}

	public void setReceivedDate(Date receivedDate) {
		this.receivedDate = receivedDate;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public String dumpPart(Part p) throws Exception {
		if (p.isMimeType("text/plain")) {
			return (String) p.getContent();
		} else if (p.isMimeType("multipart/*")) {
			Multipart mp = (Multipart) p.getContent();
			int count = mp.getCount();
			StringBuilder str = new StringBuilder();
			for (int i = 0; i < count; i++)
				str.append(dumpPart(mp.getBodyPart(i)));
			return str.toString();
		} else if (p.isMimeType("message/rfc822")) {
			return dumpPart((Part) p.getContent());
		} else {
			Object o = p.getContent();
			if (o instanceof String) {
				return o.toString();
//			} else if (o instanceof InputStream) {
//				InputStream is = (InputStream) o;
//				int c;
//				StringBuilder str = new StringBuilder();
//				while ((c = is.read()) != -1) {
//					str.append(c);
//				}
//				return str.toString();
			} else {
				return o.toString();
			}
		}

//		/*
//		 * If we're saving attachments, write out anything that looks like an
//		 * attachment into an appropriately named file. Don't overwrite existing
//		 * files to prevent mistakes.
//		 */
//		String filename = p.getFileName();
//		String disp = p.getDisposition();
//		// many mailers don't include a Content-Disposition
//		if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) {
//			if (filename == null)
//				filename = "Attachment";
//			try {
//				File f = new File(filename);
//				if (f.exists())
//					// XXX - could try a series of names
//					throw new IOException("file exists");
//				((MimeBodyPart) p).saveFile(f);
//			} catch (IOException ex) {
//			}
//		}
	}
}
